參考文件
OmniAuth Google OAuth2
Login With Google Account Using Devise and Omniauth
打開官方文件參考從Devise開始的部分
##安裝及申請
安裝gem1
gem 'omniauth-google-oauth2'
$ bundle install
1.到google console https://console.developers.google.com頁面
2.頁面最上面創立一個專案
3.啟用兩個API服務Contacts API以及Google+ API
4.左邊的選單選擇憑證申請OAuth 2.0 用戶端 ID
5.可能需要等十分鍾
##Devise的方式實作
這邊假設已經安裝好Devise並且已經用Devise創造出users這個model
修改config/initializers/devise.rb1
config.omniauth :google_oauth2, 'GOOGLE_CLIENT_ID', 'GOOGLE_CLIENT_SECRET', {}
GOOGLE_CLIENT_ID填入剛剛申請的用戶IDGOOGLE_CLIENT_SECRET填入剛剛申請的用戶密碼
這時候登入頁面最下面應該已經多一個Sign in with GoogleOauth2的連結,且登入成功後會預設去users/omniauth_callbacks這個path!
修改routes.rb加上如果google登入成功後會呼叫的path1
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
再來更新model新增一行1
devise :omniauthable, omniauth_providers: [:google_oauth2]
再來不要忘記controller
你可以放在controller/application.rb或自行創立對應路徑rails g controller users/omniauth_callbacks可以創造對應的路徑檔案
1 | class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController |
此時注意到controller裡面from_omniauth這個方法不是內建的要自行創立
再次更新model:1
2
3
4
5
6
7
8
9
10
11
12def self.from_omniauth(access_token)
data = access_token.info
user = User.where(:email => data["email"]).first
unless user
password = Devise.friendly_token[0,20]
user = User.create(email: data["email"],
password: password, password_confirmation: password
)
end
user
end
從unless開始是我自己的程式碼可以參考
給password獨特的token
然後我只記下使用者的信箱
意思是我利用google信箱登入後如果有在我的資料庫找到該位user就直接用,沒有的話就創立一個user。
此時應該就大功告成了!